//------------------------------------------------------------------------------
// <copyright file="HeroItemsControl.xaml.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using DotBeer.Business.Entities;
using DAnton.Utilities.Extensions;
using DotBeer.Business.Components;
using System.Windows.Input;

namespace DotBeer.UI.WPFClient
{
    /// <summary>
    /// Interaction logic for ItemsControl.xaml
    /// </summary>
    public partial class HeroItemsControl : UserControl
    {
        #region private members

        ItemsCollection _itemsColl;
        Style _buttonItemStyle;

        #endregion

        #region constructors

        public HeroItemsControl()
        {
            InitializeComponent();

            if (!this.IsInDesignMode())
            {
                _buttonItemStyle = (Style)FindResource("ButtonItemStyle");
                AddTabs();
            }
        }

        #endregion

        #region properties


        #endregion

        #region private methods

        #region user actions

        void tabItem_GotFocus(object sender, RoutedEventArgs e)
        {
            FillWithItems((TabItem)sender);
        }

        #endregion

        private void ItemsCollectionChanged()
        {
            for (int i = 0; i < this.TabControlItems.Items.Count; i++)
            {
                TabItem tab = (TabItem)this.TabControlItems.Items[i];
                tab.Tag = true;
            }
        }

        private void FillWithItems(TabItem tabItem)
        {
            if (_itemsColl == null)
                _itemsColl = ItemsCollectionComponent.Load();

            if ((bool)tabItem.Tag == true)
            {
                var items = _itemsColl.Items.Where(it => it.Vendor == ((Image)tabItem.Header).Tag.ToString()).Select(it => it).OrderBy(it => it.Position);

                UniformGrid grid = new UniformGrid() { Rows = 3, Columns = 4 };

                foreach (Item item in items)
                {
                    grid.Children.Add(this.GetStackPanelItem(item));
                }

                tabItem.Content = grid;
                tabItem.Tag = false;
            }
        }

        private StackPanel GetStackPanelItem(Item item)
        {
            StackPanel stackPanelItem = new StackPanel()
            {
                Orientation = Orientation.Vertical
            };

            // add item

            Button button = new Button()
            {
                Style = _buttonItemStyle,
                Tag = item,
                Width = 50,
                Height = 50,
                ToolTip = item.Name,
                HorizontalAlignment = HorizontalAlignment.Left
            };
            button.Click += (object sender, RoutedEventArgs e) =>
                {
                    ICommand command = this.GetValue(CommandProperties.CommandProperty) as ICommand;

                    if (command != null)
                        command.Execute(item);
                };

            Uri uri = new Uri(item.ImagePath);

            try
            {
                button.Content = new Image() { Source = new BitmapImage(new Uri(item.ImagePath)), Stretch = Stretch.Uniform };
            }
            catch
            {
                button.Content = new Image() { Source = new BitmapImage(new Uri(ApplicationPaths.QuestionMark)), Stretch = Stretch.Uniform };
            }

            stackPanelItem.Children.Add(button);

            return stackPanelItem;
        }

        private void AddTabs()
        {
            foreach (string name in new Vendors())
            {
                TabItem tabItem = new TabItem();
                tabItem.GotFocus += new RoutedEventHandler(tabItem_GotFocus);
                tabItem.Tag = false;
                tabItem.Style = (Style)this.FindResource("TabItemStyle");
                tabItem.Margin = new Thickness(3, 3, 3, 3);
                tabItem.Tag = true;
                tabItem.ToolTip = name;

                Image img = new Image()
                {
                    Width = 30,
                    Height = 30,
                    Stretch = Stretch.Uniform,
                    StretchDirection = StretchDirection.Both,
                    Tag = name
                };

                tabItem.Header = img;

                string fileName = String.Format(@"{0}Images\Vendors\{1}.png", AppDomain.CurrentDomain.BaseDirectory, name.Replace("/", ""));

                if (File.Exists(fileName))
                    img.Source = new BitmapImage(new Uri(fileName));
                else
                    img.Source = new BitmapImage(new Uri(ApplicationPaths.QuestionMark));

                this.TabControlItems.Items.Add(tabItem);
            }

            if (this.TabControlItems.Items.Count > 0)
                ((TabItem)this.TabControlItems.Items[0]).Focus();
        }

        #endregion
    }
}
